// i.r(t), i.d(t, { default: () => CanvasExtModule })
import { PlayerMovedMessage, PlayerResizedMessage } from "../message/play.message"
import Module from "./Module"
import CanvasData from "../data/CanvasData"
import { ScreenToCanvasPointCommand } from "../command/screen.command"
import { BlurCanvasCommand, ResizeCanvasCommand } from "../command/canvas.command"
export default class CanvasExtModule extends Module {
  _canvas: any
  msgBus: any
  canvasBounds: any
  data: CanvasData
  constructor() {
    super(...arguments), (this.name = "canvas-ext")
  }
  async init(e, t) {
    this._canvas = e.canvas
    this.msgBus = t.msgBus
    this.canvasBounds = this._canvas.getBoundingClientRect()
    this.bindings.push(
      t.commandBinder.addBinding(ResizeCanvasCommand, async function () {}),
      t.commandBinder.addBinding(ScreenToCanvasPointCommand, async function () {}),
      t.commandBinder.addBinding(BlurCanvasCommand, async function () {})
    )
    this.data = new CanvasData(this.canvasBounds.width, this.canvasBounds.height)
    t.market.register(this, CanvasData, this.data)
  }
  get element() {
    return this._canvas
  }
  get width() {
    return this.canvasBounds.width
  }
  get height() {
    return this.canvasBounds.height
  }
  get x() {
    return this.canvasBounds.left
  }
  get y() {
    return this.canvasBounds.top
  }
  onUpdate() {
    this.canvasBounds = this._canvas.getBoundingClientRect()
    const e = this.data.width !== this.width || this.data.height !== this.height,
      t = this.data.x !== this.x || this.data.y !== this.y
    e &&
      ((this.data.width = this.width),
      (this.data.height = this.height),
      this.data.commit(),
      this.msgBus.broadcast(new PlayerResizedMessage(this.width, this.height)))
    t && ((this.data.x = this.x), (this.data.y = this.y), this.data.commit(), this.msgBus.broadcast(new PlayerMovedMessage(this.x, this.y)))
  }
}
